// pages/member-neighbor/member-neighbor.js
import api from '../../utils/api.js'
let app = getApp();
import {
    HTTP
} from "../../utils/http.js";
import { config } from '../../utils/config'
let http = new HTTP();
Page({

    /** 
     * 页面的初始数据
     */
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        shuffled: [], //随机
        test: [], //拿到社区 所有的团
        showModel: false, // 模态窗口
        showDetail: false, //展示 规则
        authorizeBtn: false,
        isJoin: 0,
        imgUrl: config.imgUrl,
        // organizationList:false
    },
    // 展示规则
    showDetail(e) {
        let showDetail = this.data.showDetail;
        let showModel = this.data.showModel;

        this.setData({
            showDetail: !showDetail,
            showModel: !showModel
        })

    },
    close(e) {
        this.setData({
            productDetail: [],
            details: [],
            showDetail: false,
            showModel: false
        })
    },

    //申请团长
    goApplyHeader() {
        wx.navigateTo({
            url: "/pages/member-applyHeader/member-applyHeader",
        })
    },
    //拿到用户小区的其他团
    getAllOrganizationList() {
        let e = null
        let id = wx.getStorageSync('user_id')
        let city = wx.getStorageSync('city_id')
        let city_id = city
        http.request({
            url: api.getAllOrList,
            data: {
                id,
                city_id
            },
            success: (res) => {
                console.log('拿到用户小区的其他团', res);
                if (res.error_code == 40001) {
                    this.setData({
                        organizationList: false
                    })
                } else if (res.code == 200) {
                    let test = res.data;
                    //  console.log(test)
                    let shuffled = this.changeBatch(e, test, 2)
                    this.setData({
                        organizationList: true,
                        test, //社区内所有的团
                        shuffled: shuffled
                    })
                    // console.log('test', test, 'shuffled', shuffled)
                }
            }
        });

    },
    //社区团————换一批,洗牌
    changeBatch(e, arr = this.data.test, count) {
        // if (arr.length){
        //   return false;
        // }
        count = count || 2;
        let shuffled = arr.slice(0), //第一个
            i = arr.length, //数组长度
            min = i - count, // min是剩余几个
            temp, index;
        while (i-- > min) {
            index = Math.floor((i + 1) * Math.random());
            temp = shuffled[index];
            shuffled[index] = shuffled[i];
            shuffled[i] = temp;
        }
        shuffled = shuffled.slice(min);
        console.log('shuffled', shuffled)

        // this.setData({
        //   shuffled
        // })
        return shuffled
    },
    /**用户申请入团
     * id————用户ID
     * rid————社团拥有者ID
     * tname————社团名
     * uname————用户名
     * organization_id————团id
     * apply_or_invite: 1——申请  2——邀请
     */
    userJoinOrganization(e) {
        let self = this;
        let user_id = wx.getStorageSync('user_id');
        let rid = e.currentTarget.dataset.rid;
        let tname = e.currentTarget.dataset.tname;
        let uname = e.currentTarget.dataset.uname;
        let organization_id = e.currentTarget.dataset.organization_id;

        http.request({
            url: api.userJoingototuan,
            data: {
                id: user_id,
                rid,
                tname,
                uname,
                organization_id,
                apply_or_invite: 1
            },
            success: (res) => {
                console.log('11111111111', res);
                if (res.code == 200) {
                    this.setData({
                        isJoin: 1
                    })
                    wx.showToast({
                        title: '申请成功',
                        icon: 'success',
                        duration: 1500,
                        mask: true,
                        success: () => {
                            setTimeout(() => {
                                self.onLoad()
                            }, 2000)
                        }
                    })
                } else if (res.error_code == 20001) {
                    let msg = res.msg;
                    console.log(msg);
                    wx.showToast({
                        title: "您已经加入其他团",
                        icon: 'none',
                        mask: true,
                        duration: 1500,
                        success: () => { }
                    })
                } else if (res.error_code == 40008) {
                    wx.showToast({
                        title: "申请中..",
                        icon: 'none',
                        mask: true,
                        duration: 1500,
                        success: () => { }
                    })
                }
            }
        });
    },

    onLoad: function (options) {
        this.userShareEnter(options)
        let nickName = wx.getStorageSync('nickName')
        this.setData({
            nickName
        })

    },
    userShareEnter(options) {
        console.log('options', options)
        //由分享进入时，别人进入你的分享时
        if (options.typeShare == 1) {
            let nickName = wx.getStorageSync('nickName');
            //若用户已授权
            if (nickName) {
                //存在，即已经获得授权
                this.setData({
                    share: false
                })
            } else {
                //没获得授权时， 显示授权按钮
                this.setData({
                    authorizeBtn: true
                })
            }
        }

        wx.updateShareMenu({
            withShareTicket: true
        })

    },
    hideShareMenu() {
        wx.hideShareMenu({
            success: () => {
                console.log('隐藏 成功')
            },
            complete: () => { }
        })
    },
    onShow: function () {
        this.isUserOrganizationStatus()
    },
    /**拿到用户的 ————————团状态
     * 200,'该用户既没有创建团也没有加入团   
     * 205,'该用户已创建团也加入了其他团 
     * 210,'该用户创建团 但是没有加入团
     * 215,'该用户没创建团 但是加入了团
     * 
     * create_organization_id": 2,   //创建的团ID
     * "join_organization_id": 0      //加入的团id  0代表没有加入任何团
     */
    isUserOrganizationStatus() {
        let id = wx.getStorageSync('user_id')
        http.request({
            url: api.isUserOrganizationStatus,
            data: {
                id
            },
            success: (res) => {
                console.log('11111111111', res);
                let code = res.code
                this.getAllOrganizationList()

                let create_organization_id = res.data.create_organization_id;
                let join_organization_id = res.data.join_organization_id;
                this.setData({
                    code,
                    create_organization_id,
                    join_organization_id
                })
            }
        });
    },

    //监听子组件 时间onMyEvent
    onMyEvent: function (e) {
        console.log('eeeeeeeeeeeeeeee', e)

        let group_id = e.detail.id; //点击或滑动切换时 ，当前团的id；
        this.setData({
            group_id
        })
    },

    onShareAppMessage: function (res) {
        console.log('res', res)
        if (res.from === 'menu') { //button  menu
            var content = {
                title: `为彼此不再陌生创和谐社区！邻居组团买菜、买水果送年猪了~`,
                imageUrl: 'https://images.weigeee.com/20190823/37f00a0662e35b6059ab3fe058364aba.png',
                path: `/pages/index/index?redirect=/pages/member-neighbor/member-neighbor&typeShare=1`,
                success: function (res) {
                    // 转发成功
                    console.log("转发成功:" + JSON.stringify(res));
                },
                fail: function (res) {
                    // 转发失败
                    console.log("转发失败:" + JSON.stringify(res));
                }
            }
        }
        return content
    }
})